package com.my.demo.controller.enterprise;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.demo.pojo.EnterpriseUser;
import com.my.demo.pojo.Result;
import com.my.demo.pojo.User;
import com.my.demo.service.EnterpriseSpaceService;
import com.my.demo.service.EnterpriseUserService;
import com.my.demo.service.PermissonService;
import com.my.demo.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Role;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//添加企业空间成员
@RestController
public class AddEnterpriseUserController {
    @Autowired
    EnterpriseUserService enterpriseUserService;
    @Autowired
    EnterpriseSpaceService enterpriseSpaceService;
    @Autowired
    UserService userService;
    @Autowired
    PermissonService permissonService;

   /*
    * 通过电话号码搜索到成员，输出成员的信息，返回给前端用户的用户名，手机号以及用户ID
    */
    @RequiresRoles("creater")
    @PostMapping("search_enterprise_user")
    @CrossOrigin
    public Result searchEnterpriseUser(@RequestBody Map<String,String> map){
        Result result = new Result<>();
        result.setSuccess(false);
        result.setMsg(null);

        String phone=(String) map.get("phone");
        if (phone==null||phone.equals("")){
            return result;
        }
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("user_name",phone);

        User user=null;
        try {
            user=userService.getOne(userQueryWrapper);
        }catch (Exception exception){
            exception.printStackTrace();
        }

        if (user==null){
            return result;
        }

        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("phone",user.getUserName());
        resultMap.put("userName",user.getUserName());
        resultMap.put("userId",user.getUserId());

        result.setMsg("查找成功");
        result.setDetail(resultMap);
        result.setSuccess(true);
        return result;
    }

   /*
    * 前端发送数据userId以及选择的对应的用户的角色,将对应用户的userId加入到企业空间中
    * 要注意看该用户是否已经在企业空间中了，如果在应该要报错,告知用户已经存在
    */
    @RequiresRoles("creater")
    @PostMapping("add_enterprise_user")
    @CrossOrigin
    public Result addEnterpriseUser(@RequestBody Map<String,Object> map){
        Result result = new Result<>();
        result.setSuccess(false);
        result.setMsg(null);

        String scope = (String) map.get("scope");
        Integer type=0;
        if (scope.equals("2")){
            type=2;
        }
        else if (scope.equals("3")){
            type=3;
        }
        else {
            return result;
        }

        Integer userId = (Integer) map.get("userId");


        String userName = (String) SecurityUtils.getSubject().getPrincipal();
        Integer spaceId = permissonService.getById(userService.selectIdByName(userName)).getSpaceNo();



        if (userId==null||spaceId==null){
            return result;
        }

        //查找企业空间中是否已经存在该用户了
        QueryWrapper<EnterpriseUser> enterpriseUserQueryWrapper = new QueryWrapper<>();
        enterpriseUserQueryWrapper.eq("user_no",userId);
        enterpriseUserQueryWrapper.eq("space_no",spaceId);
        List<EnterpriseUser> enterpriseUserList = null;
        try {
           enterpriseUserList=enterpriseUserService.list(enterpriseUserQueryWrapper);
        }catch (Exception exception){
            exception.printStackTrace();
        }
        if (enterpriseUserList==null||enterpriseUserList.isEmpty()){
            enterpriseUserService.save(new EnterpriseUser(userId,spaceId,type));
            result.setSuccess(true);
            result.setMsg("添加用户成功");
        }
        result.setMsg("用户已经存在");
        return result;
    }
}
